package cn.takovh.zpl.bean;

import org.dom4j.Attribute;
import org.dom4j.Element;

import java.util.Iterator;

public class Media implements ElementBean {
    private String src;
    private String albumTitle;
    private String albumArtist;
    private String trackTitle;
    private String trackArtist;
    private int duration;

    public Media() {

    }

    public Media(String src, String albumTitle, String albumArtist, String trackTitle, String trackArtist, int duration) {
        this.src = src;
        this.albumTitle = albumTitle;
        this.albumArtist = albumArtist;
        this.trackTitle = trackTitle;
        this.trackArtist = trackArtist;
        this.duration = duration;
    }

    public String getSrc() {
        return src;
    }

    public void setSrc(String src) {
        this.src = src;
    }

    public String getAlbumTitle() {
        return albumTitle;
    }

    public void setAlbumTitle(String albumTitle) {
        this.albumTitle = albumTitle;
    }

    public String getAlbumArtist() {
        return albumArtist;
    }

    public void setAlbumArtist(String albumArtist) {
        this.albumArtist = albumArtist;
    }

    public String getTrackTitle() {
        return trackTitle;
    }

    public void setTrackTitle(String trackTitle) {
        this.trackTitle = trackTitle;
    }

    public String getTrackArtist() {
        return trackArtist;
    }

    public void setTrackArtist(String trackArtist) {
        this.trackArtist = trackArtist;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    @Override
    public void getElement(Element root) {
        root.addAttribute("src", src);
        root.addAttribute("albumTitle", albumTitle);
        root.addAttribute("albumArtist", albumArtist);
        root.addAttribute("trackTitle", trackTitle);
        root.addAttribute("trackArtist", trackArtist);
        root.addAttribute("duration", String.valueOf(duration));
    }

    @Override
    public void parseElement(Element root) {
        for (Iterator<Attribute> it = root.attributeIterator(); it.hasNext(); ) {
            Attribute attribute = it.next();
            String name = attribute.getName();
            String value = attribute.getValue();
            switch (name) {
                case "src":
                    this.src = value;
                    break;
                case "albumTitle":
                    this.albumTitle = value;
                    break;
                case "albumArtist":
                    this.albumArtist = value;
                    break;
                case "trackTitle":
                    this.trackTitle = value;
                    break;
                case "trackArtist":
                    this.trackArtist = value;
                    break;
                case "duration":
                    this.duration = Integer.valueOf(value);
                    break;
                default:
                    throw new IllegalArgumentException(name);
            }
        }
    }

    @Override
    public String toString() {
        return "Media{" +
                "src='" + src + '\'' +
                ", albumTitle='" + albumTitle + '\'' +
                ", albumArtist='" + albumArtist + '\'' +
                ", trackTitle='" + trackTitle + '\'' +
                ", trackArtist='" + trackArtist + '\'' +
                ", duration=" + duration +
                '}';
    }
}
